位置更改时不更新java XML数据
我正在构建一个应用程序,在屏幕上更新某些信息之后。我已经添加了xML和Java的代码,如果有人能告诉我文本视图的id和Java代码是否正确,我将不胜感激
XML
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓" xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent" 安卓:layout_height="match_parent" 安卓:paddingLeft="@dimen/activity_horizontal_margin" 安卓:paddingRight="@dimen/activity_horizontal_margin" 安卓:paddingTop="@dimen/activity_vertical_margin" 安卓:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 安卓:padding="0dp">
<ImageView 安卓:layout_width="fill_parent" 安卓:layout_height="fill_parent" 安卓:id="@+id/imageView" 安卓:layout_alignParentTop="true" 安卓:layout_alignParentStart="true" 安卓:src="@drawable/background" 安卓:scaleType="centerCrop" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Hiker's Watch" 安卓:id="@+id/textView" 安卓:layout_alignParentTop="true" 安卓:layout_centerHorizontal="true" 安卓:textSize="30sp" 安卓:textColor="#f3202020" 安卓:layout_marginTop="30dp" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Latitude: " 安卓:id="@+id/lat" 安卓:layout_below="@+id/textView" 安卓:layout_centerHorizontal="true" 安卓:layout_marginTop="30dp" 安卓:textSize="20sp" 安卓:textColor="#151515" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Longitude:" 安卓:id="@+id/lng" 安卓:layout_below="@+id/lat" 安卓:layout_centerHorizontal="true" 安卓:layout_marginTop="10dp" 安卓:textSize="20sp" 安卓:textColor="#151515" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Accuracy: 20.0m" 安卓:id="@+id/accuracy" 安卓:layout_below="@+id/lng" 安卓:layout_centerHorizontal="true" 安卓:layout_marginTop="10dp" 安卓:textSize="20sp" 安卓:textColor="#151515" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Speed:" 安卓:id="@+id/speed" 安卓:layout_below="@+id/accuracy" 安卓:layout_centerHorizontal="true" 安卓:layout_marginTop="20dp" 安卓:textSize="20sp" 安卓:textColor="#151515" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Bearing: " 安卓:id="@+id/bearing" 安卓:layout_below="@+id/speed" 安卓:layout_centerHorizontal="true" 安卓:layout_marginTop="10dp" 安卓:textSize="20sp" 安卓:textColor="#151515" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Altitude:" 安卓:id="@+id/altitude" 安卓:layout_below="@+id/bearing" 安卓:layout_centerHorizontal="true" 安卓:layout_marginTop="20dp" 安卓:textSize="20sp" 安卓:textColor="#151515" />
<TextView 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:text="Address: \n" 安卓:id="@+id/address" 安卓:layout_below="@+id/altitude" 安卓:layout_centerHorizontal="true" 安卓:layout_marginTop="20dp" 安卓:textSize="20sp" 安卓:textColor="#151515" 安卓:gravity="center_horizontal" />
</RelativeLayout>
Java
import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.location.Address;
import 安卓.location.Criteria;
import 安卓.location.Geocoder;
import 安卓.location.Location;
import 安卓.location.LocationListener;
import 安卓.location.LocationManager;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends Activity implements LocationListener {
LocationManager locationManager;
String provider;
TextView latTV;
TextView lngTV;
TextView accuracyTV;
TextView speedTV;
TextView bearingTV;
TextView altitudeTV;
TextView addressTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latTV = (TextView) findViewById(R.id.lat);
lngTV = (TextView) findViewById(R.id.lng);
accuracyTV = (TextView) findViewById(R.id.accuracy);
speedTV = (TextView) findViewById(R.id.speed);
bearingTV = (TextView) findViewById(R.id.bearing);
altitudeTV = (TextView) findViewById(R.id.altitude);
addressTV = (TextView) findViewById(R.id.address);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Double alt = location.getAltitude();
Float bearing = location.getBearing();
Float speed = location.getSpeed();
Float accuracy = location.getAccuracy();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(lat, lng, 1);
if (listAddresses != null && listAddresses.size() > 0 ) {
Log.i("PlaceInfo", listAddresses.get(0).toString());
String addressHolder = "";
for (int i = 0; i <= listAddresses.get(0).getMaxAddressLineIndex(); i++) {
addressHolder += listAddresses.get(0).getAddressLine(i) + "\n";
}
addressTV.setText("Address:\n" + addressHolder);
}
} catch (IOException e) {
e.printStackTrace();
}
latTV.setText("Latitude: " + lat.toString());
lngTV.setText("Longitude: " + lng.toString());
altitudeTV.setText("Altitude: " + alt.toString() + "m");
bearingTV.setText("Bearing: " + bearing.toString());
speedTV.setText("Speed: " + speed.toString() + "m/s");
accuracyTV.setText("Accuracy: " + accuracy.toString() + "m");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
# 1 楼答案
您对
Double
和Float
的String
解析在.setText();
内不正确更正:
# 2 楼答案
您应该使用Fused location api
下面是一个详细示例:http://www.androidwarriors.com/2015/10/fused-location-provider-in-android.html
关于
Fused location provider API
最好的一点是,您不必担心location
更新,它总是在间隔或location
更改后为您提供最新和准确的location
更新关于
Fused location provider API
的另一件事是,您不需要考虑最佳location
提供商,因为它会自动选择最适合您的android设备硬件的提供商